package com.qiwx.pigeon.data.network;

import com.qiwx.pigeon.BuildConfig;
import com.qiwx.pigeon.base.Constant;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-16 13:26
 **/
public class ApiManager {
    public static ApiService apiService;
    private static final long TIMEOUT = 30000;


    /**
     * 初始化网络连接
     */
    public static ApiService initApiServer(ApiService service, String url) {
        if (service == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                    .readTimeout(TIMEOUT, TimeUnit.MILLISECONDS)
                    .writeTimeout(TIMEOUT, TimeUnit.MILLISECONDS);
            if (BuildConfig.DEBUG) {//release 版 必须取消日志
                builder.addInterceptor(new CustomerOkHttpClient.LoggingInterceptor());
            }
            OkHttpClient client = builder.build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .client(client)
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            service = retrofit.create(ApiService.class);
        }
        return service;
    }

    public static ApiService getApiServer() {
        return initApiServer(apiService, BuildConfig.BASE_URL);
    }

}
